﻿<template>
  <a-modal
    title="编辑Issue"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="Project ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-select style="width: 100%" placeholder="请选择Project ID" v-decorator="['projectId', {rules: [{ required: true, message: '请选择Project ID！' }]}]">
            <a-select-option v-for="(item,index) in  ProjectData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="Priority" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择Priority" v-decorator="['priority', {rules: [{ required: true, message: '请选择Priority！' }]}]">
            <a-select-option v-for="(item,index) in priorityData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="WorkloadTillNow" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入WorkloadTillNow" v-decorator="['workloadTillNow']" />
        </a-form-item>
        <a-form-item label="DueDate" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择DueDate" v-decorator="['dueDate']" @change="onChangedueDate"/>
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    IssueFkProjectList,
    IssueEdit
  } from '@/api/modular/main/IssueManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        priorityData: [],
        dueDateDateString: '',
        ProjectData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      IssueFkProjectList() {
          IssueFkProjectList().then(res => {
            this.ProjectData = res.data
          })
      },
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        this.record = record
        const priorityOption = this.$options
        this.priorityData = priorityOption.filters['dictData']('')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              projectId: record.projectId,
              priority: record.priority,
              workloadTillNow: record.workloadTillNow
            }
          )
        }, 100)
        this.form.getFieldDecorator('dueDate', { initialValue: moment(record.dueDate, 'YYYY-MM-DD') })
        this.dueDateDateString = moment(record.dueDate).format('YYYY-MM-DD')
      this.IssueFkProjectList()
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            values.dueDate = this.dueDateDateString
            this.record.dueDate = this.dueDateDateString
            IssueEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangedueDate(date, dateString) {
        this.dueDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
